package org.op.menu;

import java.awt.Frame;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class OpFileChooser {

	private Boolean foldersOnly = false;
	private Frame topFrame;
	private String fromPath;
	private String approveButtonText;
	private String title;
	private FileFilter filter;
	
	public OpFileChooser(Frame container) {
		foldersOnly = false;
		topFrame = container;
	}
	
	public OpFileChooser(Frame container, String path) {
		fromPath = path;
	}
	
	public File openAndGet() {
		
		File file = null;
		
		JFileChooser fc = null;
		if (fromPath!=null) {
			fc = new JFileChooser(fromPath);
		} else {
			fc = new JFileChooser();
		}
		
		if(foldersOnly) {
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}
		
		if (approveButtonText!=null) {
			fc.setApproveButtonText(approveButtonText);
		}
		
		if (title!=null) {
			fc.setDialogTitle(title);
		}
		
		if (filter!=null) {
			fc.setFileFilter(filter);
		}
		
		int returnVal = fc.showOpenDialog(topFrame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = fc.getSelectedFile();
            //This is where a real application would open the file.
            System.out.println("Opening: " + file.getName() + "." );
        } else {
            System.out.println("Open command cancelled by user.");
        }
        
        return file;

	}

	public Boolean getFoldersOnly() {
		return foldersOnly;
	}

	public void setFoldersOnly(Boolean foldersOnly) {
		this.foldersOnly = foldersOnly;
	}

	public String getFromPath() {
		return fromPath;
	}

	public void setFromPath(String fromPath) {
		this.fromPath = fromPath;
	}

	public String getApproveButtonText() {
		return approveButtonText;
	}

	public void setApproveButtonText(String approveButtonText) {
		this.approveButtonText = approveButtonText;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public FileFilter getFilter() {
		return filter;
	}

	public void setFilter(FileFilter filter) {
		this.filter = filter;
	}
	
	
}
